from sqlalchemy.ext.asyncio import AsyncSession
from typing import List
from config.constant import CommonConstant
from exceptions.exception import ServiceException
from module_admin.entity.vo.common_vo import CrudResponseModel
from module_monitor.dao.monitor_dao import MonitorDao
from module_monitor.entity.vo.monitor_vo import DeleteMonitorModel, MonitorModel, MonitorPageQueryModel
from utils.common_util import CamelCaseUtil
from utils.excel_util import ExcelUtil


class MonitorService:
    """
    监控管理模块服务层
    """

    @classmethod
    async def get_monitor_list_services(
        cls, query_db: AsyncSession, query_object: MonitorPageQueryModel, is_page: bool = False
    ):
        """
        获取监控管理列表信息service

        :param query_db: orm对象
        :param query_object: 查询参数对象
        :param is_page: 是否开启分页
        :return: 监控管理列表信息对象
        """
        monitor_list_result = await MonitorDao.get_monitor_list(query_db, query_object, is_page)

        return monitor_list_result


    @classmethod
    async def add_monitor_services(cls, query_db: AsyncSession, page_object: MonitorModel):
        """
        新增监控管理信息service

        :param query_db: orm对象
        :param page_object: 新增监控管理对象
        :return: 新增监控管理校验结果
        """
        try:
            await MonitorDao.add_monitor_dao(query_db, page_object)
            await query_db.commit()
            return CrudResponseModel(is_success=True, message='新增成功')
        except Exception as e:
            await query_db.rollback()
            raise e

    @classmethod
    async def edit_monitor_services(cls, query_db: AsyncSession, page_object: MonitorModel):
        """
        编辑监控管理信息service

        :param query_db: orm对象
        :param page_object: 编辑监控管理对象
        :return: 编辑监控管理校验结果
        """
        edit_monitor = page_object.model_dump(exclude_unset=True, exclude={'create_by', 'create_time', })
        monitor_info = await cls.monitor_detail_services(query_db, page_object.monitor_id)
        if monitor_info.monitor_id:
            try:
                await MonitorDao.edit_monitor_dao(query_db, edit_monitor)
                await query_db.commit()
                return CrudResponseModel(is_success=True, message='更新成功')
            except Exception as e:
                await query_db.rollback()
                raise e
        else:
            raise ServiceException(message='监控管理不存在')

    @classmethod
    async def delete_monitor_services(cls, query_db: AsyncSession, page_object: DeleteMonitorModel):
        """
        删除监控管理信息service

        :param query_db: orm对象
        :param page_object: 删除监控管理对象
        :return: 删除监控管理校验结果
        """
        if page_object.monitor_ids:
            monitor_id_list = page_object.monitor_ids.split(',')
            try:
                for monitor_id in monitor_id_list:
                    await MonitorDao.delete_monitor_dao(query_db, MonitorModel(monitorId=monitor_id))
                await query_db.commit()
                return CrudResponseModel(is_success=True, message='删除成功')
            except Exception as e:
                await query_db.rollback()
                raise e
        else:
            raise ServiceException(message='传入为空')

    @classmethod
    async def monitor_detail_services(cls, query_db: AsyncSession, monitor_id: int):
        """
        获取监控管理详细信息service

        :param query_db: orm对象
        :param monitor_id: 
        :return: 对应的信息
        """
        monitor = await MonitorDao.get_monitor_detail_by_id(query_db, monitor_id=monitor_id)
        if monitor:
            result = MonitorModel(**CamelCaseUtil.transform_result(monitor))
        else:
            result = MonitorModel(**dict())

        return result

    @staticmethod
    async def export_monitor_list_services(monitor_list: List):
        """
        导出监控管理信息service

        :param monitor_list: 监控管理信息列表
        :return: 监控管理信息对应excel的二进制数据
        """
        # 创建一个映射字典，将英文键映射到中文键
        mapping_dict = {
            'monitorId': '',
            'deviceId': '设备ID',
            'monitorType': '监控类型',
            'fileUrl': '监控文件地址',
            'fileType': '监控文件类型',
            'fileName': '监控文件名',
            'createBy': '',
            'createTime': '',
            'updateBy': '',
            'updateTime': '',
        }
        binary_data = ExcelUtil.export_list2excel(monitor_list, mapping_dict)

        return binary_data
